/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.house.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.customer.dao.IOrganizationDao;
import com.ce.pms.customer.model.OrganizationDO;
import com.ce.pms.house.dao.ICoWorkerDao;
import com.ce.pms.house.model.CoWorkerDomain;
import com.ce.pms.house.service.ICoWorkerService;
import com.ce.pms.house.vo.CoWorkerVO;
import com.ce.pms.exception.BizLayerException;
import com.ce.pms.exception.BusinessException;
import com.sitechasia.webx.core.support.Page;

/** 功能描述：
 * @author 罗军林 创建日期：2008-6-30
 */
public class CoWorkerServiceImpl extends PmsBaseServiceImpl implements ICoWorkerService {
	private ICoWorkerDao coWorkerDao;
	private IOrganizationDao organizationDao;

	/* (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#addCoWorker(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public CoWorkerDomain addCoWorker(CoWorkerVO coWorkerVO) {

		checkNotNullOption(coWorkerVO);
		checkName(coWorkerVO,null);

	//	checkParticipantIsCoWorker(coWorkerVO);

		SysInfo sysInfo=this.createInfo(coWorkerVO.getCreator());
		SysrefcodeDO sysrefcodeDO=this.sysrefcodeDao.getSysrefCodesByCode(coWorkerVO.getCoworkerTypeCode(),
									coWorkerVO.getCoworkerTypeValue());

		OrganizationDO organizationDO = getOrganizationDO(coWorkerVO, sysInfo);

		CoWorkerDomain coWorkerDomain=new CoWorkerDomain(organizationDO,sysInfo,sysrefcodeDO);
		return coWorkerDao.saveObject(coWorkerDomain);
	}


	/* (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#deleteCoWorkerByID(java.lang.Long)
	 */
	public void deleteCoWorkerByID(Long id) {
		checkID(id);
		coWorkerDao.deleteObjectById(id);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#editCoWorkerByID(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public CoWorkerDomain editCoWorker(CoWorkerVO coWorkerVO) {

		//数据检查
		checkNotNullOption(coWorkerVO);
		checkID(coWorkerVO.getCoWorkerID());

		//根据协作伙伴ID，查询到得CoWorkerDomain对象
		CoWorkerDomain coWorkerDomain=coWorkerDao.findById(coWorkerVO.getCoWorkerID());
		if(! coWorkerVO.getName().equals(coWorkerDomain.getParticipant().getParticipantName()))
		{
			checkName(coWorkerVO,coWorkerDomain.getParticipant().getParticipantName());
		}
		this.modifyInfo(coWorkerDomain.getSysInfo(), coWorkerVO.getCreator());
		//根据CoWorkerDomain中的参与人ID查询该协作伙伴的ID
		OrganizationDO organizationDO=organizationDao.findById(coWorkerDomain.getParticipant().getParticipantId());
		this.modifyInfo(organizationDO.getSysInfo(),coWorkerVO.getCreator());

		//开始修改
		convertOrgVoToDoForEdit(coWorkerVO, organizationDO);

		return coWorkerDomain;

	}

	/* (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#findCoWorkerByID(java.lang.Long)
	 */
	public CoWorkerVO findCoWorkerByID(Long id){
		checkID(id);
		CoWorkerDomain coWorkerDomain=coWorkerDao.findById(id);
		return getCoWorkerVOByCoWorkerDomain(coWorkerDomain);
	}


	/* (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#queryCoWorker(com.ce.pms.house.vo.CoWorkerVO, int, int)
	 */
	public Page queryCoWorker(CoWorkerVO queryCondition, int pageNo,int pageSize) {
		Page page=coWorkerDao.queryCoWorker(queryCondition, pageNo, pageSize);
		List list=page.getResult();
		List<CoWorkerVO> tempList=new ArrayList<CoWorkerVO>();
		for( int i=0;i<list.size();i++ ){
			Object[] item=(Object[]) list.get(i);
			OrganizationDO organizationDO = getOrganizationDO(item);
			CoWorkerDomain coWorkerDomain = getCoWorkerDomain(item);

			CoWorkerVO coWorkerVO=convertDOtoVO(organizationDO, coWorkerDomain);

			tempList.add(coWorkerVO);
		}
		page.setResult(tempList);
		return page;
	}


	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#getCoWorkerVOByCoWorkerDomain(com.ce.pms.house.model.CoWorkerDomain)
	 */
	public CoWorkerVO getCoWorkerVOByCoWorkerDomain(CoWorkerDomain coWorkerDomain) {
		if(coWorkerDomain!=null && coWorkerDomain.getParticipant().getParticipantId()!=null){
			OrganizationDO organizationDO=organizationDao.findById(coWorkerDomain.getParticipant().getParticipantId());
			return convertDOtoVO(organizationDO, coWorkerDomain);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#countCoWorkerByName(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public Long countCoWorkerByName(CoWorkerVO queryCondition,String oldName){
		return coWorkerDao.countCoWorkerByName(queryCondition,oldName);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#countCoWorkerByParticipantId(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public Long countCoWorkerByParticipantId(CoWorkerVO queryCondition){
		return coWorkerDao.countCoWorkerByParticipantId(queryCondition);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.house.service.ICoWorkerService#queryAllCoWorkerByCoWorkerType(com.ce.pms.house.vo.CoWorkerVO)
	 */
	public List queryAllCoWorkerByCoWorkerType(CoWorkerVO queryCondition) {
		List list=coWorkerDao.queryAllCoWorkerByCoWorkerType(queryCondition);

		List<CoWorkerVO> tempList=new ArrayList<CoWorkerVO>();
		for( int i=0;i<list.size();i++ ){
			Object[] item=(Object[]) list.get(i);
			OrganizationDO organizationDO = getOrganizationDO(item);
			CoWorkerDomain coWorkerDomain = getCoWorkerDomain(item);

			CoWorkerVO coWorkerVO=convertDOtoVO(organizationDO, coWorkerDomain);

			tempList.add(coWorkerVO);
		}
		return tempList;
	}
/*************私有方法*************************************/

	/** 功能描述:检查VO中的必填项是否为空，如果有一项为空将抛出业务异常
	 * @param coWorkerVO
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private void checkNotNullOption(CoWorkerVO coWorkerVO) {
		if(null == coWorkerVO.getCreator()
				|| null==coWorkerVO.getCoworkerTypeCode()
				|| null==coWorkerVO.getCoworkerTypeValue()
				|| null==coWorkerVO.getName())
		{
			throw new BusinessException("house:mustFill_null_exception",BusinessException.WARNING);
		}
	}

	/**
	 *  功能描述:DO转VO；因CoWorkerVO对象的数据存放于OrganizationDO、CoWorkerDomain中，所以转换前需要先传入这两个对象<br>
	 *  		传入VO的值有：协作伙伴ID，组织地址，联系人，组织名称，联系电话，邮编
	 * @param organizationDO
	 * @param coWorkerDomain
	 * @return
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private CoWorkerVO convertDOtoVO(OrganizationDO organizationDO, CoWorkerDomain coWorkerDomain) {
		CoWorkerVO coWorkerVO=new CoWorkerVO();
		coWorkerVO.setCoWorkerID(coWorkerDomain.getParticipantRoleId());
		coWorkerVO.setAddress(organizationDO.getAddress());
		coWorkerVO.setContactor(organizationDO.getContactor());
		coWorkerVO.setName(organizationDO.getParticipantName());
		coWorkerVO.setTelephone(organizationDO.getTelephone());
		coWorkerVO.setZip(organizationDO.getZipcode());
		return coWorkerVO;
	}

	/** 功能描述:根据对象数组得到协作伙伴对象
	 * @param item
	 * @return
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private CoWorkerDomain getCoWorkerDomain(Object[] item) {
		CoWorkerDomain coWorkerDomain=null;
		if(item[0] instanceof CoWorkerDomain){
			coWorkerDomain=(CoWorkerDomain) item[0];
		}else if(item[1] instanceof CoWorkerDomain){
			coWorkerDomain=(CoWorkerDomain) item[1];
		}
		return coWorkerDomain;
	}

	/** 功能描述:根据对象数组得到组织对象
	 * @param item
	 * @return
	 * Create author:罗军林
	 * Create on:2008-7-2
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private OrganizationDO getOrganizationDO(Object[] item) {
		OrganizationDO organizationDO=null;
		if(item[0] instanceof OrganizationDO){
			organizationDO=(OrganizationDO) item[0];
		}else if(item[1] instanceof OrganizationDO){
			organizationDO=(OrganizationDO) item[1];
		}
		return organizationDO;
	}

	/**
	 * 功能描述:如果id为空,则抛出异常
	 * @param id
	 * Create author:罗军林
	 * Create on:2008-6-27
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private void checkID(Long id){
		if(null==id){
			throw new BusinessException("common:parameters_null_exception,ID",BusinessException.WARNING);
		}
	}

	/** 功能描述:检查coWorkerVO中的名字唯一性;
	 * @param coWorkerVO
	 * @param count
	 * Create author:罗军林<br>
	 * Create on:2008-7-3<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkName(CoWorkerVO coWorkerVO,String oldName) {
		if(countCoWorkerByName(coWorkerVO,oldName)>0){
			throw new BusinessException("house:same_org_exist_exception,"+coWorkerVO.getName(),BusinessException.WARNING);
		}
	}

	/**
	 * 功能描述:检查coWorkerVO中的名字唯一性<br>
	 * 使用目的：一个参与人不能多次为某一个角色;<br>
	 * 此处将用到coWorkerVO中的三个字段：coworkerTypeValue,coworkerTypeCode,participantId
	 * @param coWorkerVO
	 * @param count
	 * Create author:罗军林<br>
	 * Create on:2008-7-3<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkParticipantIsCoWorker(CoWorkerVO coWorkerVO) {
		if(countCoWorkerByParticipantId(coWorkerVO)>0){
			throw new BusinessException("同一参与人不能多次为某一角色,不能继续操作",BusinessException.WARNING);
		}
	}


	/**
	 * 功能描述:将组织信息的值传到do中,这里只传送:协作伙伴名称,协作伙伴电话,联系人,协作伙伴地址,协作伙伴邮编，协作伙伴类型，系统信息
	 * @param coWorkerVO
	 * @param organizationDO
	 * Create author:罗军林
	 * Create on:2008-6-30
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private OrganizationDO convertOrgVoToDoForAdd(CoWorkerVO coWorkerVO,SysInfo sysInfo) {
		OrganizationDO organizationDO=new OrganizationDO();
		convertOrgVoToDoForEdit(coWorkerVO, organizationDO);
		organizationDO.setSysInfo(sysInfo);
		return organizationDO;
	}

	/** 功能描述:将VO中的值传入到DO中,这里只传参与人名称，参与人电话，联系人，参与人地址，参与人邮编
	 * @param coWorkerVO
	 * @param sysInfo
	 * @param organizationDO
	 * Create author:罗军林
	 * Create on:2008-7-1
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	private void convertOrgVoToDoForEdit(CoWorkerVO coWorkerVO,OrganizationDO organizationDO) {
		organizationDO.setParticipantName(coWorkerVO.getName());//协作伙伴名称
		organizationDO.setTelephone(coWorkerVO.getTelephone());//协作伙伴电话
		organizationDO.setContactor(coWorkerVO.getContactor());//联系人
		organizationDO.setAddress(coWorkerVO.getAddress());//协作伙伴地址
		organizationDO.setZipcode(coWorkerVO.getZip());//协作伙伴邮编
	}

	/** 功能描述:<p>如果coWorkerVO中的参与人ID为NULL，则新建一个organizationDO，否则根据参与人ID查询得到一个
	 * 持久化对象organizationDO；然后再将VO中的值传入到DO中</p>
	 * @param coWorkerVO
	 * @param sysInfo
	 * @return
	 * Create author:罗军林<br>
	 * Create on:2008-7-4<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private OrganizationDO getOrganizationDO(CoWorkerVO coWorkerVO, SysInfo sysInfo) {
		OrganizationDO organizationDO=null;
		if(coWorkerVO.getParticipantId()!=null){
			organizationDO=organizationDao.findById(coWorkerVO.getParticipantId());
			convertOrgVoToDoForEdit(coWorkerVO, organizationDO);
		}else{
			organizationDO=convertOrgVoToDoForAdd(coWorkerVO, sysInfo);
		}
		return organizationDO;
	}
	/*************get/set方法*************************************/
	public ICoWorkerDao getCoWorkerDao() {
		return coWorkerDao;
	}

	public void setCoWorkerDao(ICoWorkerDao coWorkerDao) {
		this.coWorkerDao = coWorkerDao;
	}

	public IOrganizationDao getOrganizationDao() {
		return organizationDao;
	}

	public void setOrganizationDao(IOrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

}
